% Compute outcomes for broader set of devaluations contained in Figure X.
% Induce reductions in T which generate desired wage reduction.
close all
clc
disp('Expenditure switching')

NN =6; Nb_models =2;
Wage_reduc   =zeros(Nb_models,NN);
TBS_delta_vec = zeros(Nb_models,NN);

dT=[0.0335 0.0499 0.0660 0.086 0.1125 0.140; 0.0358 0.0538 0.077 0.108  0.15   0.205];

%% Loop over models
for j=1:Nb_models

if j==1
    glob= 1; 
    ngf = 0;  
elseif j==2
    glob= 0; 
    ngf = 1;
elseif j==3
    glob = 0;
    ngf = 0;
end
%% Parameters

parameters

%% Distribution of efficiency and fixed costs of importing and exporting

% Standard normals for productivity and fixed costs
if monly==1
    load Heterogeneity_Importing.mat
else
     load Heterogeneity.mat
end

phi = DrawProductivity(U,param);
fc = DrawFixedCostsImporting(param,V,phi);
fx = DrawFixedCostsExporting(param,Z,phi,fc);

%% Target moments in data
param.shareN = 0.3870; param.shareM = 10.6939/100;

%% Optimal domestic shares and international strategy

optimalshares

%% Targeted moments in the model and statistics on distributions of sales, import and export shares

moments_nodisplay

%% Calibrate varrho and T

% Given distribution of import and export shares, find weight of imported
% final good in preferences (varrho), pre-devaluation wage (w) and transfers
% (T). See calibration appendix.

x0 = [0.1,0.2,0.15];
[w,~,~, varrho,Timplied] = calibrateaNM_byHand(sD, sX, phi,fc,fx,param);
param.varrho = varrho; param.T = Timplied;

%% Retrieve aggregate domestic spending (S), price index (P) and other equilibrium objects

[SP,SP2,P,RX,M,Iw, sN, sM,Ptot,LN] = GEobjects(w,sD, sX, phi,fc,fx, param); % SP,SP2,P are in tradables;

MF= (1-sN)*sM * Iw;  % final good imports in domestic labor
TotalExports_norm = TotalExports * (P/w)^((sg-1)*(1-g) ) * (SP/w) * knorm; % exports in domestic labor
TotalImports_norm = TotalImports * (P/w)^((sg-1)*(1-g) ) * (SP/w) * knorm + MF; % total imports in domestic labor
R = SP + RX*w; % total sales in tradables
zvec =z; yvec=y;
disp('Check balanced trade condition pre devaluation')   
disp(((TotalImports_norm)/(TotalExports_norm +param.T)-1)*100) 

%% Retrieve fixed costs:
[fc_norm,fx_norm,FX_norm,FM_norm,FXM_norm] = NormalizedFC(fc, fx, FX,FM,FXM,SP,P,w,param);

% loop over devaluations:
for i=1:size(dT,2)
    param.T = Timplied - dT(j,i);

    if glob==1 
    [x_sol, MSE] = DevaluationFixedPoint(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm, SDM,SDXM,z,y, P, SP,w,Opt_options);
    end
    if ngf==1
    [x_sol, MSE] = DevaluationFixedPoint(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm, SDM,SDXM,z,y, P, SP,w,Opt_options);
    end    
    
    Peq = abs(x_sol(1)); Seq = abs(x_sol(2)); weq = abs(x_sol(3));
            
    [PriceChange, Mn, RXn,EqMoments,sD1,omega1,sX1,TotalExports_norm1,TotalImports_norm1,SPn,In,wn,sNn,sMn,Ptotn,LNn] = DevaluationResults(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm, SDM,SDXM,z,y, P,Peq,Seq,weq);
    
    Rn = SPn + RXn*wn; 
    MFn= (1-sNn)*sMn * In;
    wage_growth(i) = (wn/w-1)*100;
    ExpoS = TotalExports_norm*w/R; % Exports normalized by Sales
    ExpoS1(i) = TotalExports_norm1*wn/Rn;
    ImpoS = (TotalImports_norm-MF)*w/R; 
    ImpoS1(i) = (TotalImports_norm1-MFn)*wn/Rn;
    DS = (ExpoS - ImpoS)*100; DS1(i) = (ExpoS1(i) - ImpoS1(i))*100;
    TBS_delta(i)= DS1(i)-DS;ExpoS_delta(i)= ExpoS1(i) - ExpoS; ImpoS_delta(i)= ImpoS1(i) - ImpoS;
end

Wage_reduc(j,:)   = wage_growth;
TBS_delta_vec(j,:)= TBS_delta;
end

Wage_reduc_SAVED = Wage_reduc;

TBS_gap = TBS_delta_vec(1,:)-TBS_delta_vec(2,:);

% Figure with TB:
Wage_reduc =[-15 -20 -25 -30 -35 -40;-15 -20 -25 -30 -35 -40;-15 -20 -25 -30 -35 -40];
figure
set(gcf,'color','w');
plot(-Wage_reduc(1,1:end),TBS_delta_vec(1,1:end),'blue','LineWidth',3.0,'Marker','o','MarkerSize',7);
hold on
plot(-Wage_reduc(2,1:end),TBS_delta_vec(2,1:end),'red','LineWidth',3.0,'Marker','^','MarkerSize',7)
ylabel('Difference in TB/S (pp)','Fontsize',20)
xlabel('Wage reduction (%)','Fontsize',20)
legend('Baseline','No Global Firms','Orientation','Horizontal','Fontsize',20,'Location','South');
legend boxoff

% Statistic for main text:
cd ../../Output/
h=gcf;
set(h,'PaperPositionMode','auto');         
set(h,'PaperOrientation','landscape');
set(h,'Position',[50 50 1200 800]);
print(gcf,'-bestfit', '-dpdf', 'F10.pdf')
csvwrite('Stat_in_Text_ExpSwitch.csv',TBS_gap(6));
cd ../QuantitativeModel/mfiles
